package com.mobile.web.dict;

import com.mobile.service.dto.DataDictionaryDTO;
import com.mobile.service.dto.DataDictionaryQueryDTO;
import com.mobile.service.inter.system.DictService;
import com.mobile.web.base.BaseController;
import com.mobile.web.config.util.Jurisdiction;
import com.mobile.util.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 数据字典API接口
 */
@RestController
@RequestMapping("/dict")
public class DictController extends BaseController {
    @Autowired
    private DictService dictService;

    /**
     * 数据字典列表（分页）
     * @return
     */
    @PostMapping(value = "/getDictListPage")
    //@RequiresPermissions("dicmana:read:1")
    public Object getDictListPage(DataDictionaryQueryDTO dataDictionaryQueryDTO) {
        Map<String, Object> map = new HashMap<>();

        try {
            PageResult<DataDictionaryDTO> dictPage = dictService.getDictListPage(dataDictionaryQueryDTO);
            map.put("page", dictPage);
            map.put("data", dictPage.getPageDatas());
            map.put("QX", Jurisdiction.getHC(this.getUserCode(),"dicmana"));
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_SUCCESS);
        } catch (Exception e) {
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_EXCEPTION);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0016"));
            logger.error(e.getMessage(), e);
        }

        return AppUtil.returnObject(map);
    }

    /**
     * 根据字典ID查询
     * @return
     */
    @PostMapping(value = "/getDictByNo")
    //@RequiresPermissions("dicmana:edit:1")
    public Object getDictByNo() {
        PageData pd = getPageData();
        Map<String, Object> map = new HashMap<>();

        try {
            PageData dict = dictService.getDictByNo(pd.getString("dicNo"));
            if (Objects.isNull(dict)) {
                map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_FAILED);
                map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0001"));
                return map;
            }
            map.put("data",dict);
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_SUCCESS);
        } catch (Exception e) {
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_EXCEPTION);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0016"));
            logger.error(e.getMessage(), e);
        }

        return AppUtil.returnObject(pd, map);
    }

    /**
     * 添加字典
     * @return
     */
    @PostMapping(value = "/addDict")
    //@RequiresPermissions("dicmana:add:1")
    public Object addDict() {
        Map<String, Object> map;
        PageData pd = this.getPageData();

        try {
            map = dictService.addDict(pd);
        } catch (Exception e) {
            map = new HashMap<>();
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_EXCEPTION);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0016"));
            logger.error(e.getMessage(), e);
        }

        return AppUtil.returnObject(pd, map);
    }

    /**
     * 修改字典
     * @return
     */
    @PostMapping(value = "/updateDict")
    //@RequiresPermissions("dicmana:edit:1")
    public Object updateDict() {
        Map<String, Object> map = new HashMap<>();
        PageData pd = this.getPageData();

        try {
            map = dictService.updateDict(pd);
        } catch (Exception e) {
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_EXCEPTION);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0016"));
            logger.error(e.getMessage(), e);
        }

        return AppUtil.returnObject(pd, map);
    }

    /**
     * 删除字典
     * @return
     */
    @PostMapping(value = "/removeDict")
    //@RequiresPermissions("dicmana:delete:1")
    public Object removeDict() {
        Map<String, Object> map;
        PageData pd = this.getPageData();

        try {
            map = dictService.removeDict(pd);
        } catch (Exception e) {
            map = new HashMap<>();
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_EXCEPTION);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0016"));
            logger.error(e.getMessage(), e);
        }

        return AppUtil.returnObject(pd, map);
    }

    /**
     * 添加字典项
     * @return
     */
    @PostMapping(value = "/addDictItem")
    //@RequiresPermissions("dicmana:add:1")
    public Object addDictItem() {
        Map<String, Object> map;
        PageData pd = this.getPageData();

        try {
            map = dictService.addDictItem(pd);
        } catch (Exception e) {
            map = new HashMap<>();
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_EXCEPTION);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0016"));
            logger.error(e.getMessage(), e);
        }

        return AppUtil.returnObject(pd, map);
    }


    /**
     * 根据字典ID和字典项值查询字典项
     * @return
     */
    @GetMapping(value = "/get/{dicNo}")
    public Object get(@PathVariable String dicNo) {
        Map<String, Object> map = new HashMap<>();
        try {
            List<PageData> dicItems = dictService.getDictItemByNoPre(dicNo);
            map.put("data", dicItems);
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_SUCCESS);
        } catch (Exception e) {
            map = new HashMap<>();
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_EXCEPTION);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0016"));
            logger.error(e.getMessage(), e);
        }

        return AppUtil.returnObject(map);
    }
    /**
     * 根据字典ID和字典项值查询字典项
     * @return
     */
    @PostMapping(value = "/getDictItemByNoAndValue")
    //@RequiresPermissions("dicmana:edit:1")
    public Object getDictItemByNoAndValue() {
        Map<String, Object> map = new HashMap<>();
        PageData pd = this.getPageData();
        try {
            PageData data = dictService.getDictItemByNoAndValue(pd);
            if (Objects.isNull(data)) {
                map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_FAILED);
                map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0001"));
                return map;
            }
            map.put("data", data);
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_SUCCESS);
        } catch (Exception e) {
            map = new HashMap<>();
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_EXCEPTION);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0016"));
            logger.error(e.getMessage(), e);
        }

        return AppUtil.returnObject(pd, map);
    }

    /**
     * 修改字典项
     * @return
     */
    @PostMapping(value = "/updateDictItem")
    //@RequiresPermissions("dicmana:edit:1")
    public Object updateDictItem() {
        Map<String, Object> map = null;
        PageData pd = this.getPageData();

        try {
            map = dictService.updateDictItem(pd);
        } catch (Exception e) {
            map = new HashMap<>();
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_EXCEPTION);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0016"));
            logger.error(e.getMessage(), e);
        }

        return AppUtil.returnObject(pd, map);
    }

    /**
     * 删除字典项
     * @return
     */
    @PostMapping(value = "/removeDictItem")
    //@RequiresPermissions("dicmana:delete:1")
    public Object removeDictItem() {
        Map<String, Object> map = new HashMap<>();
        PageData pd = this.getPageData();

        try {
            dictService.removeDictItem(pd);
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_SUCCESS);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("I0010"));
        } catch (Exception e) {
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_EXCEPTION);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0016"));
            logger.error(e.getMessage(), e);
        }

        return AppUtil.returnObject(pd, map);
    }

    /**
     * 根据字典ID查询所有字典项值
     * @return
     */
    @PostMapping(value = "/getDictItemByNo")
    public Object getDictItemByNo() {
        Map<String, Object> map = new HashMap<>();
        PageData pd = this.getPageData();

        try {
            List<PageData> dicItems = dictService.getDictItemByNo(pd.getString("dicNo"));
            map.put("data", dicItems);
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_SUCCESS);
        } catch (Exception e) {
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_EXCEPTION);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0016"));
            logger.error(e.getMessage(), e);
        }

        return AppUtil.returnObject(pd, map);
    }

    /**
     * 根据字典ID批量查询字典项
     * @return
     */
    @RequestMapping(value = "/getDictItemBatch")
    public Object getDictItemBatch() {
        Map<String, Object> map = new HashMap<>();
        PageData pd = this.getPageData();

        try {
            String dicNoList = pd.getString("dicNoList");
            if (StringUtil.isEmpty(dicNoList)) {
                map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_SUCCESS);
                return AppUtil.returnObject(pd, map);
            }
            for (String dicNo : dicNoList.split(",")) {
                map.put(dicNo, dictService.getDictItemByNo(dicNo));
            }
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_SUCCESS);
        } catch (Exception e) {
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_EXCEPTION);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0016"));
            logger.error(e.getMessage(), e);
        }

        return AppUtil.returnObject(pd, map);
    }

    /**
     * 根据字典ID和拓展字段4查询所有字典项值
     * @return
     */
    @PostMapping(value = "/getDictItemByNoAndExtendFour")
    public Object getDictItemByNoAndExtendFour() {
        Map<String, Object> map = new HashMap<>();
        PageData pd = this.getPageData();

        try {
            List<PageData> dicItems = dictService.getDictItemByNoAndExtendFour(pd);
            map.put("data", dicItems);
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_SUCCESS);
        } catch (Exception e) {
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_EXCEPTION);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0016"));
            logger.error(e.getMessage(), e);
        }

        return AppUtil.returnObject(pd, map);
    }

}
